﻿using MCPlatform.Dxf.Utils;
using netDxf.Entities;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
using System.Windows.Shapes;
using static netDxf.Entities.HatchBoundaryPath;

namespace MCPlatform.Dxf.Entities
{
    public class Polyline2DPathEntity : PathEntityBase
    {

       
        public Polyline2D Polyline2D { get; }


        public Polyline2DPathEntity(Polyline2D polyline2D)
        {
            Polyline2D = polyline2D;
            Type = polyline2D;
            Handle = polyline2D.Handle;
            CalculatePoint(polyline2D);
            DrawShape(polyline2D);
            GetPoint(polyline2D);
        }

        public void DrawShape(Polyline2D? polyLine, float scaleX = 1, float scaleY = 1, float strokeThickness =1)
        {
            // 如果 polyLine 为空，则使用默认的 polyLine
            if (polyLine == null)
            {
                polyLine = this.Polyline2D;
            }
         
            var lines = ConvertPolylineToLines(polyLine, scaleX, scaleY, strokeThickness);

            this.Shape = ConvertLinesToPath(lines,strokeThickness);

            this.Shape.Tag = polyLine.Handle;

        }

        public List<System.Windows.Shapes.Line> ConvertPolylineToLines(Polyline2D polyline, float scaleX = 1, float scaleY = 1, float strokeThickness =1)
        {
            List<System.Windows.Shapes.Line> lines = new List<System.Windows.Shapes.Line>();

            for (int i = 0; i < polyline.Vertexes.Count - 1; i++)
            {
                var startVertex = polyline.Vertexes[i];
                var endVertex = polyline.Vertexes[i + 1];

                System.Windows.Shapes.Line line = new System.Windows.Shapes.Line
                {
                    X1 = startVertex.Position.X * scaleX,
                    Y1 = startVertex.Position.Y * scaleY,
                    X2 = endVertex.Position.X * scaleX,
                    Y2 = endVertex.Position.Y * scaleY,
                    Stroke = Utility.ConvertAciColorToBrush(polyline.Color),
                    StrokeThickness = Utility.GetNumberFromTextToFloat(polyline.Lineweight.ToString(), polyline.Layer.Lineweight.ToString()) ?? strokeThickness,
                };
                lines.Add(line);

                
            }

            // If polyline is closed, connect the last vertex to the first
            if (polyline.IsClosed)
            {
                var startVertex = polyline.Vertexes[polyline.Vertexes.Count - 1];
                var endVertex = polyline.Vertexes[0];

                System.Windows.Shapes.Line line = new System.Windows.Shapes.Line
                {
                    X1 = startVertex.Position.X * scaleX,
                    Y1 = startVertex.Position.Y * scaleY,
                    X2 = endVertex.Position.X * scaleX,
                    Y2 = endVertex.Position.Y * scaleY,
                    Stroke = Utility.ConvertAciColorToBrush(polyline.Color),
                    StrokeThickness = Utility.GetNumberFromTextToFloat(polyline.Lineweight.ToString(), polyline.Layer.Lineweight.ToString()) ?? strokeThickness,
                };

                lines.Add(line);
            }

            return lines;
        }

        public Path ConvertLinesToPath(List<System.Windows.Shapes.Line> lines, float strokeThickness = 1)
        {
            PathGeometry pathGeometry = new PathGeometry();
            PathFigure pathFigure = new PathFigure();

            if (lines.Count > 0)
            {
                // Initialize the starting point of the PathFigure
                pathFigure.StartPoint = new System.Windows.Point(lines[0].X1, lines[0].Y1);

                foreach (var line in lines)
                {
                    // Create a LineSegment for each Line
                    LineSegment lineSegment = new LineSegment(new System.Windows.Point(line.X2, line.Y2), true);
                    pathFigure.Segments.Add(lineSegment);
                }

                pathGeometry.Figures.Add(pathFigure);
            }

            Path path = new Path
            {
                Data = pathGeometry,
                Stroke = lines.Count > 0 ? lines[0].Stroke : Brushes.Black,
                StrokeThickness = strokeThickness
            };

            return path;
        }

        public void CalculatePoint(Polyline2D polyLine)
        {
            if (polyLine.Vertexes.Count > 0)
            {
                StartPoint = new netDxf.Vector3()
                {
                    X = polyLine.Vertexes[0].Position.X,
                    Y = polyLine.Vertexes[0].Position.Y,
                };


                int middleIndex = polyLine.Vertexes.Count / 2;

                MidPoint = new netDxf.Vector3()
                {
                    X = polyLine.Vertexes.ElementAt(middleIndex).Position.X,
                    Y = polyLine.Vertexes.ElementAt(middleIndex).Position.Y,
                };




                EndPoint = new netDxf.Vector3()
                {
                    X = polyLine.Vertexes.Last().Position.X,
                    Y = polyLine.Vertexes.Last().Position.Y,
                };

                if (polyLine.IsClosed)
                {
                    EndPoint = StartPoint;
                    
                }
            }
        }

        public void GetPoint(Polyline2D polyLine)
        {
            if (polyLine.Vertexes.Count > 0)
            {

                PointsList = new List<netDxf.Vector3>();    
                foreach (var vertex in polyLine.Vertexes)
                {
                    var Point = new netDxf.Vector3()
                    {
                        X = vertex.Position.X,
                        Y = vertex.Position.Y,
                        Z = 0
                    };
                      
                    PointsList.Add(Point);
                }
                
                

                if (polyLine.IsClosed)
                {
                    var Point = new netDxf.Vector3()
                    {
                        X = polyLine.Vertexes[0].Position.X,
                        Y = polyLine.Vertexes[0].Position.Y,
                        Z = 0
                    };
                   
                    PointsList.Add(Point);

                }
            }
        }

    }
}
